//
//  SHEmoticonPageViewCell.swift
//  SHWeibo3
//
//  Created by teacher on 16/7/29.
//  Copyright © 2016年 teacher. All rights reserved.
//

import UIKit

class SHEmoticonPageViewCell: UICollectionViewCell {
    // 保存20个按钮数组
    var buttons:[SHEmoticonButton] = [SHEmoticonButton]()
    
    // 定义一个装有模型的数组
    var emoticons: [SHEmoticon]?{
        didSet{
            
            
            // 隐藏所有按钮
            for value in buttons {
                value.hidden = true
            }
            
            
            // 遍历表情数组
            for (i,emoticon) in emoticons!.enumerate() {
                // 得到对应的button
                let button = buttons[i]
                // 赋值
                button.emoticon = emoticon
                // 显示按钮
                button.hidden = false
                // 代表是emoji表情
                if emoticon.isEmoji {
                    button.setTitle((emoticon.code ?? "" as NSString).emoji(), forState: UIControlState.Normal)
                    // 解决image和title 重复问题
                    button.setImage(nil, forState: UIControlState.Normal)
                }else {
                    // 代表图片表情
                    let image = UIImage.getEmoticonImage(emoticon.path)
                    button.setImage(image, forState: UIControlState.Normal)
                    // 解决image和title 重复问题
                    button.setTitle(nil, forState: UIControlState.Normal)
                }
                
                
            }
            
        }
    }
    
    var indexPath: NSIndexPath?{
        didSet{
            messageLabel.text = "第\(indexPath?.section ?? 0)组\n第\(indexPath?.item ?? 0)页"
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 监听事件
    // 删除按钮点击发送通知
    func deleteButtonClick(){
        // 发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBEmoticonDeleteButtonNoti, object: nil)
    }
    // 表情按钮点击发送通知
    func buttonClick(button: SHEmoticonButton) {
        NSNotificationCenter.defaultCenter().postNotificationName(WBEmoticonButtonsNoti, object: button.emoticon)
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        addChildButtons()
        contentView.addSubview(deleteButton)
//        // 添加控件
//        contentView.addSubview(messageLabel)
//        // 添加约束
//        messageLabel.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(contentView)
//        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // 按钮的宽高
        let childW = (self.frame.width - 10) / CGFloat(SHEmoticonMaxCol)
        let childH = (self.frame.height - 20) / CGFloat(SHEmoticonMaxRow)
        // 遍历按钮数组 给button设置frame
        for (i,value) in buttons.enumerate() {
            let col = CGFloat(i%SHEmoticonMaxCol)
            let row = CGFloat(i/SHEmoticonMaxCol)
            value.frame = CGRect(x: childW*col + 5, y: childH*row, width: childW, height: childH)
        }
        // 设置删除按钮的frame
        deleteButton.frame = CGRect(x: self.frame.width - childW - 5, y: childH*2, width: childW, height: childH)
    }
    
    // 添加按钮
    func addChildButtons(){
        for _ in 0..<SHEmoticonMaxCount {
            let button = SHEmoticonButton()
            // 添加点击事件
            button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            button.titleLabel?.font = UIFont.systemFontOfSize(30)
            contentView.addSubview(button)
            buttons.append(button)
        }
    }
    
    // MARK: - 懒加载控件
    private lazy var messageLabel: UILabel = {
        let lab = UILabel(textColor: UIColor.redColor(), textFont: 35, maxWidth: SCREENW)
        lab.textAlignment = .Center
        return lab
    }()
    // 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        // 添加事件
        button.addTarget(self, action: "deleteButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        return button
    }()
    
}




